package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisVOService;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.vo.TravelStatisTotalVO;
import cn.wolfcode.luowowo.cache.vo.UserStatisVO;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.member.vo.UserVO;
import cn.wolfcode.luowowo.search.domain.UserInfoTemplate;
import cn.wolfcode.luowowo.search.service.IUserInfoEsService;
import cn.wolfcode.luowowo.website.annotion.RequireLogin;
import cn.wolfcode.luowowo.website.annotion.UserParam;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import cn.wolfcode.luowowo.website.util.UploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/mine")
public class UserInfoController {

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;

    @Reference
    private ITravelStatisVOService travelStatisVOService;

    @Reference
    private ITravelService travelService;

    @Reference
    private IUserInfoEsService userInfoEsService;

    @Reference
    private IStrategyService strategyService;

    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    @Value("${file.path}")
    private String filePath;

    @RequestMapping("/home")
    @RequireLogin
    public String home(Model model, @UserParam UserInfo userInfo){
        Long focusCount = userInfoRedisService.getFocusCount(userInfo.getId());
        Long fansCount = userInfoRedisService.getFansCount(userInfo.getId());
        List<UserStatisVO> statisVOList = userInfoRedisService.getStatisVOList(userInfo.getId());
        List<Travel> travelList = travelService.getTravelListByUserId(userInfo.getId());
        model.addAttribute("focusCount",focusCount);
        model.addAttribute("fansCount",fansCount);
        model.addAttribute("focusList",statisVOList);
        model.addAttribute("travelList",travelList);
        return "mine/homepage";
    }

    @RequestMapping("/setting")
    @RequireLogin
    public String setting(Model model,@UserParam UserInfo userInfo){
        model.addAttribute("userInfo",userInfo);
        return "mine/setting";
    }


    @RequestMapping("/updateUserInfo")
    @ResponseBody
    @RequireLogin
    public Object updateUserInfo(UserInfo userInfo, HttpServletRequest request, HttpServletResponse response){
        System.out.println(userInfo);
        userInfoService.updateUserInfo(userInfo);
        this.resetUserToken(request, response, userInfo);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/uploadHeadImg")
    @ResponseBody
    @RequireLogin
    public Object uploadHeadImg(MultipartFile pic){
        String upload = UploadUtil.upload(pic, filePath);
        return upload;
    }


    @RequestMapping("/updateHeadImgUrl")
    @ResponseBody
    @RequireLogin
    public Object updateHeadImgUrl(UserInfo userInfo,HttpServletRequest request,HttpServletResponse response){
        userInfoService.updateHeadImgUrl(userInfo);
        this.resetUserToken(request, response, userInfo);
        return AjaxResult.SUCCESS;
    }


    @RequestMapping("/updatePassword")
    @ResponseBody
    @RequireLogin
    public Object updatePassword(String vPassword,String password,HttpServletRequest request,HttpServletResponse response,@UserParam UserInfo userInfo){
        userInfoService.updatePassword(vPassword,password,userInfo.getId());
        this.resetUserToken(request, response, userInfo);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/updateEmail")
    @ResponseBody
    @RequireLogin
    public Object updateEmail(String email,HttpServletRequest request,HttpServletResponse response,@UserParam UserInfo userInfo){
        userInfoService.udpateEmail(email,userInfo.getId());
        this.resetUserToken(request, response, userInfo);
        return AjaxResult.SUCCESS;
    }

    private void resetUserToken(HttpServletRequest request,HttpServletResponse response,UserInfo userInfo){
        UserInfo user = userInfoService.get(userInfo.getId());
        String token = CookieUtil.getToken(request, response);
        userInfoRedisService.setToken(token,user);
    }


    @RequestMapping("/authentication")
    @ResponseBody
    @RequireLogin
    public Object authentication(String phone,String verifyCode){
        userInfoService.authentication(phone,verifyCode);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/updatePhone")
    @ResponseBody
    @RequireLogin
    public Object updatePhone(String phone,String verifyCode,@UserParam UserInfo userInfo,HttpServletRequest request,HttpServletResponse response){
        userInfoService.authentication(phone,verifyCode);
        userInfoService.updatePhone(phone,userInfo.getId());
        this.resetUserToken(request, response, userInfo);
        return AjaxResult.SUCCESS;
    }


    @RequestMapping("/userListEs")
    @RequireLogin
    public String findUserListEs(Model model,String nickname){

        List<UserInfoTemplate> userListEs = userInfoEsService.findUserListEs(nickname);
        model.addAttribute("userListEs",userListEs);
        return "/mine/userListEsTpl";
    }

    @RequestMapping("/blackUserList")
    @RequireLogin
    public String blackUserList(Model model,@UserParam UserInfo userInfo){
        List<UserVO> blackUserList = userInfoService.blackUserList(userInfo.getId());
        model.addAttribute("blackUserList",blackUserList);
        return "mine/blackuserlist";
    }

    @RequestMapping("/addBlackUser")
    @ResponseBody
    @RequireLogin
    public Object addBlackUser(Long blackUserId,@UserParam UserInfo userInfo){
        userInfoService.addBlackUser(userInfo.getId(),blackUserId);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/deleteBlackUser")
    @ResponseBody
    @RequireLogin
    public Object deleteBlackUser(Long blackUserId,@UserParam UserInfo userInfo){
        userInfoService.deleteBlackUser(userInfo.getId(),blackUserId);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/myTravel")
    @RequireLogin
    public String myTravel(Model model, @UserParam UserInfo userInfo, @ModelAttribute("qo") TravelQuery qo){
        PageInfo pageInfo = travelService.queryForListByUserId(userInfo.getId(),qo);
        TravelStatisTotalVO totalVO = userInfoRedisService.travelStatis(userInfo.getId());
        model.addAttribute("totalVO",totalVO);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("userInfo",userInfo);
        return "/mine/mytravelnotes";
    }

    @RequestMapping("/myTravelTpl")
    @RequireLogin
    public String myTravelTpl(Model model, @UserParam UserInfo userInfo, @ModelAttribute("qo") TravelQuery qo){
        PageInfo pageInfo = travelService.queryForListByUserId(userInfo.getId(),qo);
        model.addAttribute("pageInfo",pageInfo);
        return "/mine/mytravelTpl";
    }



    @RequestMapping("/myCollection")
    @RequireLogin
    public String collection(Model model,@UserParam UserInfo userInfo){
        List<StrategyDetail> strategyDetails = strategyService.queryStrategyDetails(userInfo.getId());
        model.addAttribute("strategyDetails",strategyDetails);
        model.addAttribute("userInfo",userInfo);
        return "/mine/travelcollection";
    }



}
